<!--获取供应商数据组件-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    width="50%"
    v-dialogDrag
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="form" label-width="80px">
      <el-row :gutter="10" class="m-condition">
        <el-col :span="15">
          <el-form-item label="编码/名称">
            <el-input
              v-model="keyWord"
              placeholder="请输入关键字进行过滤"
              @input="onFilterChanged()"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3" :offset="6">
          <el-button type="primary" @click="selectedSupplier">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="m-agTableModel">
      <ag-grid-vue
        class="m-agTableModel ag-theme-balham"
        :gridOptions="gridOptions"
        :columnDefs="columnDefs"
        :rowData="supplierData"
        rowSelection="single"
        v-loading="loading"
      ></ag-grid-vue>
    </div>
    <div class="m-pageModel m-pBottom">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.current"
        :page-sizes="[30, 50, 100, 150, 200]"
        :page-size="pageInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      ></el-pagination>
    </div>
  </el-dialog>
</template>

<script>
import _Api from "@/api";
import localeText from "@/i18n/agGridText";

export default {
  data() {
    return {
      // 弹窗是否显示
      title: "",
      columnDefs: [],
      supplierData: [],
      row: [],
      name: "",
      pageInfo: {
        current: 1,
        size: 30,
        total: 0
      },
      keyWord: "",
      loading: false
    };
  },
  props: {
    layout: {
      type: String,
      default: "supplier"
    },
    dialogVisible: {
      type: Boolean
    }
  },
  beforeMount() {
    this.gridOptions = {
      context: {
        componentParent: this
      },
      rowSelection: "single",
      enableColResize: true,
      localeText: localeText,
      onRowDoubleClicked: this.rowDoubleClicked
    };
    if (this.layout === "supplier") {
      this.columnDefs = [
        { headerName: "供应商编号", field: "supplierCode" },
        { headerName: "供应商名称", field: "supplierName" }
      ];
      this.title = "选择供应商";
    } else {
      this.columnDefs = [
        { headerName: "公司编码", field: "companyCode" },
        { headerName: "公司名称", field: "companyName" }
      ];
      this.title = "选择公司";
    }
  },
  mounted() {
    this.search();
  },
  methods: {
    handleClose() {
      this.$emit("dialogClose");
    },
    //   过滤
    onFilterChanged() {
      setTimeout(() => {
        this.gridOptions.api.setQuickFilter(this.keyWord);
      }, 200);
    },
    handleSizeChange(params) {
      this.pageInfo.size = params;
      this.search();
    },
    handleCurrentChange(params) {
      this.pageInfo.current = params;
      this.search();
    },
    // 查询主数据
    search() {
      const { size, current } = this.pageInfo;
      let obj = {};
      let apiName = "";
      if (this.layout == "supplier") {
        apiName = "getSupplierInfo";
        obj = {
          state: 0,
          current: current,
          size: size,
          activeFlag: 1
        };
      } else {
        apiName = "getCompanyInfo";
        obj = {
          state: 0,
          current: current,
          size: size
        };
      }
      // this.name = this.name.replace(/\s+/g, "");
      // if (this.name) {
      //   obj.likeParams.supplierCode ='';
      //   obj.likeParams.supplierName = '';
      // }

      this.loading = true;
      _Api[apiName](obj)
        .then(response => {
          this.loading = false;
          let data = response.data;
          if (data.code == "1") {
            if (data.total > 0) {
              this.gridOptions.api.setRowData(data.rows.splice(0));
              this.pageInfo.total = data.total;
            } else {
              this.gridOptions.api.setRowData([]);
              this.pageInfo.total = 0;
            }
          } else {
            this.$alert("操作失败！:" + data.info, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            });
            this.gridOptions.api.setRowData([]);
          }
        })
        .catch(() => {
          this.loading = false;
          this.$alert("操作失败！", {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure"
          });
          this.gridOptions.api.setRowData([]);
        });
    },
    // 确认选择行数据
    selectedSupplier() {
      let row = this.gridOptions.api.getSelectedRows();
      if (row.length <= 0) {
        this.$alert("提示：请选择数据", {
          showClose: false,
          confirmButtonText: "确定",
          customClass: "alertPrompt"
        });
        return;
      }
      this.$emit("setSelectData", row[0]);
      this.handleClose();
    },
    // 双击行回调
    rowDoubleClicked() {
      this.selectedSupplier();
    }
  }
};
</script>

<style></style>
